import request from "@/utils/request";
import { UserForm, UserInfo, UserPageVO, UserQuery } from "./type";

/**
 * 登录成功后获取用户信息（昵称、头像、权限集合和角色集合）
 */
export function getUserInfoApi(): Result<UserInfo> {
  return request({
    url: "/user/getUserInfo",
    method: "get",
  });
}

/**
 * 获取用户分页列表
 *
 * @param queryParams
 */
export function getUserPage(queryParams: UserQuery): PageResult<UserPageVO> {
  return request({
    url: "/user/page",
    method: "get",
    params: queryParams,
  });
}

/**
 * 获取用户表单详情
 *
 * @param userId
 */
export function getUserForm(userId: number): Result<UserForm> {
  return request({
    url: "/user/" + userId + "/form",
    method: "get",
  });
}

/** 添加用户 */
export function addUser(data: UserForm): Result<void> {
  return request({
    url: "/user",
    method: "post",
    data: data,
  });
}

/** 修改用户 */
export function updateUser(data: UserForm): Result<void> {
  return request({
    url: "/user",
    method: "put",
    data: data,
  });
}

/** 修改用户密码 */
export function updateUserPassword(id: number, password: string): Result<void> {
  return request({
    url: "/user/" + id + "/password",
    method: "patch",
    params: { password: password },
  });
}

/** 删除用户 */
export function deleteUsers(ids: string): Result<void> {
  return request({
    url: "/user/" + ids,
    method: "delete",
  });
}

/** 下载用户导入模板 */
export function downloadTemplateApi(): Result<void> {
  return request({
    url: "/api/v1/users/template",
    method: "get",
    responseType: "arraybuffer",
  });
}

/** 导出用户 */
export function exportUser(queryParams: UserQuery): Result<void> {
  return request({
    url: "/api/v1/users/_export",
    method: "get",
    params: queryParams,
    responseType: "arraybuffer",
  });
}

/** 导入用户 */
export function importUser(deptId: number, file: File): Result<void> {
  const formData = new FormData();
  formData.append("file", file);
  return request({
    url: "/api/v1/users/_import",
    method: "post",
    params: { deptId: deptId },
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
}
